//
//  CLImageCollectionView.m
//  CLImagePicker
//
//  Created by Criss on 2017/3/17.
//  Copyright © 2017年 Criss. All rights reserved.
//

#import "CLImageCollectionView.h"
#import "CLImageCollectionCell.h"
#import "CLImageShowView.h"
#import "CLImageScrollDisplayView.h"

static NSString *CLimageCollectionCellId = @"CLimageCollectionCellId";

@interface CLImageCollectionView () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) NSArray<UIImage *> *imageArray;

@end

@implementation CLImageCollectionView

+ (instancetype)collectionWithImageArray:(NSArray<UIImage *> *)imageArray {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = [UIScreen mainScreen].bounds.size.width/ 4 ;
    layout.itemSize = CGSizeMake(width, width);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    CLImageCollectionView *imageCollectionView = [[CLImageCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout imageArray:imageArray];
    return imageCollectionView;
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout imageArray:(NSArray<UIImage *> *)imageArray{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        self.imageArray = imageArray;
        [self showUI];
    }
    return self;
}

- (void)showUI {
    self.backgroundColor = [UIColor whiteColor];
    self.dataSource = self;
    self.delegate = self;
    
    [self registerClass:[CLImageCollectionCell class] forCellWithReuseIdentifier:CLimageCollectionCellId];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CLImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CLimageCollectionCellId forIndexPath:indexPath];
    [cell setImageSelectedBlock:^(UIImage *image, CLImageCollectionCell *imageCell) {
        if (image) {
            [self.sortOrder addObject:@(indexPath.row).description];
            if (self.sortOrder.count + self.originalImagesCounts > _imageCounts) {
                [self.sortOrder removeObject:@(indexPath.row).description];
                if ([self.showAlertDelegate respondsToSelector:@selector(imageCollectionView:)]) {
                    [self.showAlertDelegate imageCollectionView:self];
                    imageCell.imageSelected = NO;
                }
            }else {
                [self.imageDicM setObject:image forKey:@(indexPath.row).description];
            }
            NSLog(@"%@", self.imageDicM);
        }else {
            [self.imageDicM removeObjectForKey:@(indexPath.row).description];
            [self.sortOrder removeObject:@(indexPath.row).description];
        }
        
        NSLog(@"%@", _sortOrder);
    }];
    if (self.imageDicM[@(indexPath.row).description]) {
        cell.imageSelected = YES;
    }
    cell.image = self.imageArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CLImageCollectionCell *cell = (CLImageCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    CGRect cellFrame = [self convertRect:cell.frame toView:[UIApplication sharedApplication].keyWindow];
    CLImageScrollDisplayView *imageShowView = [[CLImageScrollDisplayView alloc] initWithConverFrame:cellFrame index:indexPath.row willShowImages:self.imageArray];
    imageShowView.imageViewWillDismissBlock = ^CGRect(NSIndexPath *indexPath) {
        CLImageCollectionCell *dismissCell = (CLImageCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
        CGRect dismissRect = [self convertRect:dismissCell.frame toView:[UIApplication sharedApplication].keyWindow];
        return dismissRect;
    };
#warning isShowPageControl 是否展示pageControl
//    imageShowView.showPageControl = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:imageShowView];
}

#pragma mark - 懒加载
- (NSMutableDictionary *)imageDicM {
    if (!_imageDicM) {
        _imageDicM = [NSMutableDictionary dictionary];
    }
    return _imageDicM;
}

- (NSMutableArray *)sortOrder {
    if (!_sortOrder) {
        _sortOrder = [NSMutableArray array];
    }
    return _sortOrder;
}

@end
